package pucpr.pibic.funcoes;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import pucpr.pibic.casos.Caso;
import pucpr.pibic.casos.Caso2008;
import pucpr.pibic.casos.Casos2010;
public class Lucene 
{
//	public static void main(String[] args)
//	{
//		try
//		{
//			//	Specify the analyzer for tokenizing text.
//			//	The same analyzer should be used for indexing and searching
//			StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
//			//	Code to create the index
//			Directory index = new RAMDirectory();
//
//			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
//
//			IndexWriter w = new IndexWriter(index, config);
//			addDoc(w, "Lucene in Action", "193398817");
//		    addDoc(w, "Lucene for Dummies", "55320055Z");
//		    addDoc(w, "Managing Gigabytes", "55063554A");
//		    addDoc(w, "The Art of Computer Science", "9900333X");
//			w.close();
//
//			//	Text to search
//			String querystr = args.length > 0 ? args[0] : "NaMe";
//
//			//	The \"title\" arg specifies the default field to use when no field is explicitly specified in the query
//			Query q = new QueryParser(Version.LUCENE_44, "title", analyzer).parse(querystr);
//
//			// Searching code
//			int hitsPerPage = 10;
//			IndexReader reader = DirectoryReader.open(index);
//			IndexSearcher searcher = new IndexSearcher(reader);
//			TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
//			searcher.search(q, collector);
//			ScoreDoc[] hits = collector.topDocs().scoreDocs;
//
//			//	Code to display the results of search
//			System.out.println("Found " + hits.length + " hits.");
//			for(int i=0;i<hits.length;++i) 
//			{
//				int docId = hits[i].doc;
//				Document d = searcher.doc(docId);
//				System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
//			}
//
//			// reader can only be closed when there is no need to access the documents any more
//			reader.close();
//		}
//		catch(Exception e)
//		{
//			System.out.println(e.getMessage());
//		}
//	}
	private static void addDoc(IndexWriter w, String classificacao, String numeroCaso) throws IOException 
	{
		Document doc = new Document();
		doc.add(new TextField("classificacao", classificacao, Field.Store.YES));
		doc.add(new StringField("numeroCaso", numeroCaso, Field.Store.YES));
		w.addDocument(doc);
	}

	public static boolean consultarClassificacaoReicidencia(Caso casoBase, Caso novoCaso) throws ParseException{
		Directory index = new RAMDirectory();
		StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
		try{
			IndexWriter w = new IndexWriter(index, config);
			addDoc(w, casoBase.getClassificacaoReicidencia(), casoBase.getNumeroCaso());
			w.close();
			Query q = new QueryParser(Version.LUCENE_44, "classificacao", analyzer).parse(novoCaso.getClassificacaoReicidencia());
			// Searching code
			int hitsPerPage = 10;
			IndexReader reader = DirectoryReader.open(index);
			IndexSearcher searcher = new IndexSearcher(reader);
			TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
			searcher.search(q, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;
			reader.close();
			if(hits.length != 0)
				return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean consultarClassificacaoReicidencia(Casos2010 casoBase, Casos2010 novoCaso) throws ParseException{
		Directory index = new RAMDirectory();
		StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
		try{;
			IndexWriter w = new IndexWriter(index, config);
			addDoc(w, casoBase.getPrioridadeTarefa(), Long.toString(casoBase.getId()));
			w.close();
			Query q = new QueryParser(Version.LUCENE_44, "prioridade", analyzer).parse(novoCaso.getPrioridadeTarefa());
			// Searching code
			int hitsPerPage = 10;
			IndexReader reader = DirectoryReader.open(index);
			IndexSearcher searcher = new IndexSearcher(reader);
			TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
			searcher.search(q, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;
			reader.close();
			if(hits.length != 0)
				return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean consultarClassificacaoReicidencia(Caso2008 casoBase, Caso2008 novoCaso) throws ParseException{
		Directory index = new RAMDirectory();
		StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
		try{
			IndexWriter w = new IndexWriter(index, config);
			addDoc(w, casoBase.getClassificacao().replace("/", ""), Long.toString(casoBase.getId()));
			if(casoBase.getClassificacaoReicidencia() != null)
				addDoc(w, casoBase.getClassificacaoReicidencia(), Long.toString(casoBase.getId()));
			w.close();
			Query q = new QueryParser(Version.LUCENE_44, "classificacao", analyzer).parse(novoCaso.getClassificacao().replace("/", ""));
			
			// Searching code
			int hitsPerPage = 10;
			IndexReader reader = DirectoryReader.open(index);
			IndexSearcher searcher = new IndexSearcher(reader);
			TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
			searcher.search(q, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;
			reader.close();
			if(hits.length != 0)
				return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
}